package sun.springboot.quartz_scheduler_integrate.core;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import sun.springboot.quartz_scheduler_integrate.core.model.JobConf;
import sun.springboot.quartz_scheduler_integrate.core.model.TriggerConf;

import java.time.LocalDateTime;


/**
 * 调度服务--（ 0 * 14 * * ?  时间格式为例）
 * <p>
 * <p>
 * 一调度器可调度多个job，一个job也可由多个调度器调度--具体 查阅 Scheduler 方法描述
 * 当前事例为一对一
 */
@Slf4j
@Component
public class CronSchedulerServer {
    Logger logger = LoggerFactory.getLogger(CronSchedulerServer.class);
    private Scheduler scheduler;

    public CronSchedulerServer(Scheduler scheduler) {
        this.scheduler = scheduler;
    }


    /**
     * 添加
     */
    public void addScheduler(TriggerConf triggerConf, JobConf jobConf) throws SchedulerException {
        try {
            scheduler.scheduleJob(BuilderTJ.buildJobDetail(jobConf), BuilderTJ.buildCronTrigger(triggerConf));
            logger.info("{}添加调度任务成功。", LocalDateTime.now().toString());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 暂停触发器 当前事例为 一对一
     */
    public void pauseTrigger(TriggerConf triggerConf) throws SchedulerException {
        try {
            scheduler.pauseTrigger(triggerConf.getKey());
            logger.info("{}暂停触发器：{}。", LocalDateTime.now().toString(), triggerConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 暂停job
     */
    public void pauseJob(JobConf jobConf) throws SchedulerException {
        try {
            scheduler.pauseJob(jobConf.getKey());
            logger.info("{}暂停job：{}。", LocalDateTime.now().toString(), jobConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 继续触发器
     */
    public void resumeTrigger(TriggerConf triggerConf) throws SchedulerException {
        try {
            scheduler.resumeTrigger(triggerConf.getKey());
            logger.info("{}继续触发器：{}。", LocalDateTime.now().toString(), triggerConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 继续job
     */
    public void resumeJob(JobConf jobConf) throws SchedulerException {
        try {
            scheduler.resumeJob(jobConf.getKey());
            logger.info("{}继续job：{}。", LocalDateTime.now().toString(), jobConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 删除调度器
     */
    public void unScheduleJob(TriggerConf triggerConf) throws SchedulerException {
        try {
            scheduler.unscheduleJob(triggerConf.getKey());
            logger.info("{}删除调度器：{}。", LocalDateTime.now().toString(), triggerConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }

    /**
     * 删除job
     */
    public void delJob(JobConf jobConf) throws SchedulerException {
        try {
            scheduler.deleteJob(jobConf.getKey());
            logger.info("{}删除job：{}。", LocalDateTime.now().toString(), jobConf.getName());
        } catch (SchedulerException schedulerException) {
            throw schedulerException;
        }
    }


}
